package gspushmq.v1.app.subscribeapp.utils;

import java.util.Date;

import ggframework.mongodb.BasicDBObject;
import ggframework.mongodb.DBCollection;
import ggframework.mongodb.DBObject;
import ggframework.mongodb.GGMongoOperator;
import gspushmq.util.MongoUtil;
import gspushmq.v1.app.subscribeapp.enumeration.MsgStatusEnum;
import gspushmq.v1.app.subscribeapp.models.LocalDO;

/**
 * @Description 序列化数据到本地库
 * @Author chengsj
 * @Date 2018/9/21 11:40
 * @Version 1.0
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2015 SunTime Co'Ltd Inc. All rights reserved.
 **/
public class LocalSerialHelper {


    /**
     *
     * @Description 序列化消息对象到本地数据库
     * @Author chengsj
     * @Version 1.0
     *
     */
    public static void pushToLocal(LocalDO message) {
        DBCollection collection = MongoUtil.getFtMixCollection("cs_msg_app_content");
        BasicDBObject parent = GGMongoOperator.newDBObject(collection);
        message.setInsert_time(new Date());
        message.setUpdate_time(new Date());
        parent.put("id", parent.getLong("_id"));
        parent.put("sid", message.getSid());
        parent.put("plate_code", message.getPlate_code());
        parent.put("plate_name", message.getPlate_name());
        parent.put("source", message.getSource());
        parent.put("source_name", message.getSource_name());
        parent.put("message_title", message.getMessage_title());
        parent.put("message_content", message.getMessage_content());
        parent.put("message_content_ext",message.getMessage_content_ext());
        parent.put("message_img_url",message.getMessage_img_url());
        parent.put("start_time", message.getStart_time());
        parent.put("end_time", message.getEnd_time());
        parent.put("consumer",message.getConsumer());
        parent.put("consumer_code",message.getConsumer_code());
        parent.put("producer",message.getProducer());
        parent.put("producer_code",message.getProducer_code());
        parent.put("message_type_name",message.getMessage_type_name());
        parent.put("notice_link_type", message.getNotice_link_type());
        parent.put("notice_link_name", message.getNotice_link_name());
        parent.put("notice_goto_name", message.getNotice_goto_name());
        parent.put("notice_goto_url", message.getNotice_goto_url());
        parent.put("notice_goto_id", message.getNotice_goto_id());
        parent.put("notice_ext_parameters",message.getNotice_ext_parameters());
        parent.put("target", message.getTarget());
        parent.put("target_code",message.getTarget_code());
        parent.put("target_value", message.getTarget_value());
        parent.put("target_name",message.getTarget_name());
        parent.put("tags_group",message.getTags_group());
        parent.put("push_status", message.getPush_status());
        parent.put("push_status_name", message.getPush_status_name());
        parent.put("push_time", message.getPush_time());
        parent.put("auther", message.getAuther());
        parent.put("status", message.getStatus());
        parent.put("status_name", message.getStatus_name());
        parent.put("insert_time", message.getInsert_time());
        parent.put("update_time", message.getUpdate_time());
        collection.insert(parent);
    }


    /**
     *
     * @Description 取消本地数据库中保存的未推送的消息对象的状态
     * @Author chengsj
     * @Version 1.0
     *
     */
    public static void delLocal(Long id) {
        DBCollection collection = MongoUtil.getFtMixCollection("cs_msg_app_content");
        DBObject set =  new BasicDBObject("push_status", MsgStatusEnum.CANCL_PUSH.getStatus())
                .append("push_status_name",MsgStatusEnum.CANCL_PUSH.getStatusName())
                .append("update_time",new Date());
        collection.update(new BasicDBObject("id", id), new BasicDBObject("$set",set), false, true);
    }

}
